<template>
    <div class="container addmusic loading">
        <main  class="table-div">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
              <el-form-item label="角色名称" prop="rolename">
                <el-input v-model="form.rolename"></el-input>
              </el-form-item>
               <el-form-item label="角色标识" prop="roletype">
                <el-input v-model="form.roletype"></el-input>
              </el-form-item>
              <el-form-item label="角色描述" prop="roledesc">
                <el-input  type="textarea" v-model="form.roledesc"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button v-if="!id" type="primary" @click="submitAdd('ruleForm')">确认添加</el-button>
                <el-button v-if="id" type="primary" @click="submitEdit('ruleForm')">确认修改</el-button>
              </el-form-item>
            </el-form>
        </main>
    </div>
</template>

<script>
import { getRoleDetail, editRole, addRole } from '@/api/role'
export default {
  data () {
    return {
      id: '',
      form: {
        rolename: '',
        roledesc: '',
        roletype: ''
      },
      rules: {
        rolename: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roletype: [
          { required: true, message: '请输入角色标识', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
  },
  methods: {
    getDetails (id) {
      getRoleDetail({ id: id }).then(data => {
        if (data.result === 1) {
          data.data.status = data.data.status ? 1 : 0
          Object.assign(this.form, data.data)
        } else {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      })
    },
    submitAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addRole(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.back()
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    },
    submitEdit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.id = this.id
          editRole(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.replace({ name: 'RoleList', params: { page: this.oldPage }})
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    }
  },
  created () {

  },
  beforeMount () {
    this.id = this.$route.params.id
    this.oldPage = this.$route.params.page ? this.$route.params.page : ''
    if (this.id) this.getDetails(this.id)
  },
  mounted () {

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;
.table-div{
    width:90%;
    margin: 20px auto
}
.title{
    width:90%;
    margin: 10px auto;
    .search-div,.row-div{
      float: left;
    }
    .search-div{
      width: 200px;
      margin-right: 20px
    }
}
.el-upload-list__item{
  height:auto !important;
}
</style>
